package com.superpai.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.superpai.R;
import com.superpai.api.ItemAPI;
import com.superpai.common.Constants;
import com.superpai.domain.Item;
import com.superpai.response.item.SellerSearchItemListResponse;
import com.superpai.utils.StringUtils;
import com.superpai.view.adapter.ProductsAdapter;

/**
 * @author Devin.Hu
 * @date 2011-9-1
 * @version V1.0
 * @description 产品详情页面
 */
public class ProductsActivity extends BaseActivity implements OnScrollListener , OnItemClickListener {

	private SellerSearchItemListResponse response;
	private List<Item> pList;
	private String categoryId;
	private String categoryName;
	private int categoryNum;
	private int pageSize = 20;
	private int pageIndex = 1;
	private final int GETPRODUCTS = 1;
	private GridView products_list;
	private ProductsAdapter mAdapter;
	private LinearLayout footLayout;
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.products);
		initMenuView(this, mIndexBrand);
		initViews();
	}

	public void initViews(){
		
		Intent intent = this.getIntent();
		categoryId = intent.getStringExtra("categoryId"); 
		categoryName = intent.getStringExtra("categoryName");
		if(categoryName == null || categoryName.equals("")){
			categoryName = mContext.getString(R.string.menu_produts);
		}
		categoryName = StringUtils.splitString(categoryName, 15);
		title = (TextView)this.findViewById(R.id.title);
		title.setText(categoryName+"(...)");
		
		mProgress = (ProgressBar)this.findViewById(R.id.progress);
		mRefresh = (Button)this.findViewById(R.id.refresh);
		mRefresh.setOnClickListener(this);
		mNodata = (TextView)this.findViewById(R.id.nodata);
		
		footLayout = (LinearLayout)this.findViewById(R.id.load_footlayout);
		
		mAdapter = new ProductsAdapter(mContext);
		products_list = (GridView)this.findViewById(R.id.products_list);
		products_list.setAdapter(mAdapter);
		products_list.setOnScrollListener(this);
		products_list.setOnItemClickListener(this);
		
		connectionDate();
	}

    
	/**
	 * 读取店铺信息
	 */
	private void connectionDate(){
		mRefresh.setVisibility(View.GONE);
		mNodata.setVisibility(View.GONE);
		mProgress.setVisibility(View.VISIBLE);
		onConnection(GETPRODUCTS);
	}
	
	@Override
	public Object doDataConnection(int type) {
		try {
			ItemAPI api = new ItemAPI();
			response = api.sellerSearchItemList(Constants.sellerUin, pageIndex, pageSize, categoryId);
			if(response.isSuccess()){
				pList = response.getItemList();
				if(null != pList && pList.size() > 0){
					categoryNum = response.getCountTotal();
					return STATUS_SUC;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return STATUS_ERR;
		}
		return STATUS_NODATA;
	}

	@Override
	public void doProcessData(int type, Object... values) {
		int status = Integer.parseInt(values[0].toString());
		mProgress.setVisibility(View.GONE);
		if(status == STATUS_SUC){
			mAdapter.setmList(pList);
			mAdapter.notifyDataSetChanged();
			title.setText(categoryName+"("+categoryNum+")");
			footLayout.setVisibility(View.GONE);
		}else if(status == STATUS_NODATA){
			mNodata.setVisibility(View.VISIBLE);
		}else{
			mRefresh.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		checkMenu(v);
		
		if(v == mRefresh){
			connectionDate();
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if(firstVisibleItem + visibleItemCount >= totalItemCount && !mAdapter.isEmpty() && !isConnection()){
			if(connectionNextPage()){
				footLayout.setVisibility(View.VISIBLE);
			}else{
				footLayout.setVisibility(View.GONE);
			}
		}
	}
	
	/**
	 * 商品滑动到最底部事件
	 * 
	 * @return
	 * @throws
	 */
	public boolean connectionNextPage() {
		if(!isConnection()){
			if(mAdapter.getmTotalCount()< categoryNum){
				pageIndex++;
				onConnection(GETPRODUCTS);
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.putExtra("itemCode", mAdapter.getItemByPosition(position).getItemCode());
		intent.setClass(mContext, ProductDetailActivity.class);
		mContext.startActivity(intent);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		disConnection();
	}
}
